package edu.cibertec.sicsolutions.vfs.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


import edu.cibertec.sicsolutions.constants.Constants;
import edu.cibertec.sicsolutions.util.StringUtil;
import edu.cibertec.sicsolutions.vfs.domain.votacion.Consulado;
import edu.cibertec.sicsolutions.vfs.domain.votacion.Escrutinio;
import edu.cibertec.sicsolutions.vfs.domain.votacion.SessionTracking;
import edu.cibertec.sicsolutions.vfs.service.ConsuladoService;
import edu.cibertec.sicsolutions.vfs.service.EscrutinioService;
import edu.cibertec.sicsolutions.vfs.service.SessionTrackingService;

public class MonitoreoAction extends DispatchAction{

	
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	 
		Escrutinio[] listEscrutinios = EscrutinioService.resultElectoresEscrutinioByConsulado();
	 	HttpSession session = request.getSession();
		session.setAttribute("listEscrutinios",listEscrutinios);
	 	
		return mapping.findForward(Constants.SUCCESS_INIT);
	}
	
	
	public ActionForward monitoreoOnLine(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	 
		String strCodigoConsulado = request.getParameter("rowIdMonitoreo");
		if(StringUtil.isNullOrEmpty(strCodigoConsulado) ){
			return init(mapping, form, request, response);
		}
		Map map = new HashMap();
		map.put("codigoConsulado", strCodigoConsulado);
		SessionTracking[] listSessionTrackings =   SessionTrackingService.resultElectoresOnLineByConsulado(map);
		HttpSession session = request.getSession();
		if(listSessionTrackings!=null && listSessionTrackings.length>0 ){
			session.setAttribute("nombreConsulado",listSessionTrackings[0].getNombreConsulado());
		}
		
		
		session.setAttribute("listMonitoreoOnLine",listSessionTrackings);
	 	
		return mapping.findForward("onLine");
	}
	
	
}
